# 使用命令
# python .\upload.py --file=C:\Users\Administrator\Downloads\com.daofeng.peiwan_3.9.0_55_1018140800.apk --branch=167fix
# python .\upload.py --file=C:\Users\Administrator\Downloads\com.ppyuewan.peiwan_1.7.1_86_0415173711.apk --branch=167fix --changelog=test


import argparse
import requests

parser = argparse.ArgumentParser(
    description='parallel distributed runner for robot framework')
parser.add_argument('--file', nargs='?', type=str,
                    help='please set file_path')
parser.add_argument('--branch', nargs='?', type=str,
                    help='please set branch')
parser.add_argument('--changelog', nargs='?', type=str,
                    help='please set changelog')                 
args = parser.parse_args()

host = "http://112.124.59.28"

def login():
    url = "{}/api/user/login".format(host)
    data = {"username":"test","password":"Daofeng1qaz."}
 #   header = {"Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7Il9pZCI6IjYwZDU2MTAxNTMxNjUzMDAxZjk1YzNmZiIsInVzZXJuYW1lIjoidGVzdCIsImVtYWlsIjoiMTA3NTYzMDQ2N0BxcS5jb20ifSwiZXhwIjoxNjI0ODUyODQwLCJpYXQiOjE2MjQ4NDkyNDB9.np69mBL0Vh4FgewXttbpDfSQXjqK5qKAYYvHupd1nf8"}
    res = requests.post(url, data=data).json()
    item = res.get('data').get('_id')
    token = res.get('data').get('token')
    return {"item": item,"token": token }

def upload_apk():

    ## 完成登录
    # login_res = login()
    # url = '{}/api/apps/{}/upload'.format(host,login_res["item"])
    url = '{}/api/apps/{}/upload'.format(host,'6102798248d036001d7f1496')
    file_name = args.file
    #if file_name.startswith("origin/"):
    file_name = file_name.split("/")[-1]
    print(file_name)
    headers = {#"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryXLiT9FKb38w0pBfC",
            #    "Authorization": f"Bearer {login_res['token']}",
            #    "Cookie": "jenkins-timestamper-offset=-28800000; SESSIONID=90853d9a-9f27-4c60-bb5e-b8272074006c.BvgnWSobtrs-SX62dFuK1BZwjVA; request_token=82BMG11Ve4elmFik4bqcr9y2lR6r4387MQW12A7HhMeT4oKj; ltd_end=-1; pro_end=-1; serverType=nginx; order=id%20desc; memSize=15834; bt_user_info=%7B%22status%22%3Atrue%2C%22msg%22%3A%22%u83B7%u53D6%u6210%u529F%21%22%2C%22data%22%3A%7B%22username%22%3A%22181****5542%22%7D%7D; sites_path=/www/wwwroot; distribution=centos7; force=0; load_page=null; load_search=undefined; pnull=nullnot_load; rank=list; Path=/www/wwwroot; disk-unitType=KB/s; network-unitType=KB/s; backup_path=/www/backup; softType=-1; load_type=-1; ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=YWRtaW46MTYyNDYwODQ1NTc1NToxZDI4MDg4MzkxNjRmNjljMjU2ODNlN2E0MjlhMjkxZGM0OTk4YTA3Y2Q2ODM1ZjQ3NDc0ZmEyZDgyMTFlMGEw; grafana_session=24334e528777e6f01b3467a85adf4533; screenResolution=1920x1080; JSESSIONID.0f6d0d2f=node010vyziu6macof1437vrkn83w1h71.node0"
              'apiKey': '1ee364bb80acfa8d13a78fefb724732d'
              }
    files = {'file':(file_name, open(f'{args.file}', 'rb'),'application/vnd.android.package-archive')}
    branch = args.branch
    changelog = args.changelog
    res = requests.post(url, files=files, headers=headers,params={'release':True,'branch':branch,'changelog': changelog})
    print('upload:', res.text)

if __name__ == '__main__':
    upload_apk()

